<template>
  <div class="xtx-goods-page">
    <!-- 模板在没有数据时会默认渲染一次 goodDetail.categories判断是否有数据等返回的有数据了才渲染 防止vue警告查不到数据-->
    <div class="container" v-if="goodDetail.categories">
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <!-- 一级分类 -->
        <XtxBreadItem to="/">{{ goodDetail.categories[1].name }}</XtxBreadItem>
        <!-- 二级分类 -->
        <XtxBreadItem to="/">{{ goodDetail.categories[0].name }}</XtxBreadItem>
        <!-- 商品名称 -->
        <XtxBreadItem to="/">{{ goodDetail.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 商品主图 -->
        <div class="media">
          <good-img :Img="goodDetail.mainPictures"></good-img>
          <!-- 销量组件 -->
          <GoodsSales />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <!-- 信息组件 -->
          <GoodsName />
          <!-- 商品sku信息 -->
          <GoodsSku :goods="goodDetail" @change="getSku" />
          <!-- 加减按钮 -->
          <!-- vue2组件间双向数据绑定:v-model = "name"  => :value + @input  vue3组件间双向数据绑定:v-model = "name"  => :modelValue + @update:modelValue原理:子传父 父传子-->
          <XtxNumbox v-model="buyNum" :max="goodDetail.inventory"></XtxNumbox>
          <!-- 添加到购物车按钮 -->
          <XtxButton
            @click="addCart"
            style="margin-top: 20px"
            size="large"
            type="primary"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <div class="goods-tabs">
            <!-- 详情图片列表 -->
            <img
              v-for="(item, index) in goodDetail.details.pictures"
              v-imgLazy="item"
              :key="index"
            />
          </div>
        </div>
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, provide } from 'vue'
import { findGoodDetail } from '@/api/good'
import { useRoute } from 'vue-router'
import goodImg from './components/goods-image.vue'
import GoodsSales from './components/goods-sales'
import GoodsName from './components/goods-name'
import msg from '@/components/Message/index.js'
import { useStore } from 'vuex'
export default {
  name: 'XtxGoodsPage',
  components: { goodImg, GoodsSales, GoodsName },
  setup() {
    const goodDetail = ref({})
    // 传递数据
    provide('goodDetail', goodDetail)
    // vue3取参
    const route = useRoute()
    const getgoodDetail = async () => {
      const res = await findGoodDetail(route.params.id)
      goodDetail.value = res
    }
    getgoodDetail()
    // 获取sku数据
    const currSel = ref(null)
    // 购买数量
    const buyNum = ref(1)
    const getSku = (sku) => {
      console.log(sku)
      if (sku.skuId) {
        //  不是空对象就是一条有效的sku信息
        goodDetail.value.price = sku.price
        goodDetail.value.oldPrice = sku.oldPrice
        // 库存
        goodDetail.value.inventory = sku.inventory
        // 存储
        currSel.value = sku
      } else {
        // 清空
        currSel.value = null
      }
    }
    // 加入购物车
    const store = useStore()
    const addCart = async () => {
      if (!currSel.value) {
        return msg({ text: '请选择商品规格！' })
      }
      if (currSel.value.inventory === 0) {
        return msg({ text: '当前商品库存为0!' })
      }
      //  执行加入购物车
      const addGood = {
        id: goodDetail.value.id,
        name: goodDetail.value.name,
        picture: goodDetail.value.mainPictures[0],
        skuId: currSel.value.skuId,
        price: currSel.value.oldPrice,
        nowPrice: currSel.value.price,
        attrsText: currSel.value.specsText,
        stock: currSel.value.inventory,
        selected: true,
        // 是否有效商品
        isEffective: true,
        count: buyNum.value,
      }
      // 调用vuex加入购物车
      try {
        const rmsg = await store.dispatch('car/addCartAction', addGood)
        // 加入成功提示
        msg({ text: rmsg, type: 'success' })
      } catch (error) {
        console.log(error)
      }
    }

    return {
      goodDetail,
      getSku,
      buyNum,
      addCart,
    }
  },
}
</script>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background-color: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background-color: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background-color: #fff;
  img {
    width: 100%;
  }
}
.goods-warn {
  min-height: 600px;
  background-color: #fff;
  margin-top: 20px;
}
</style>
